package com.danny.controller;

import com.danny.entity.User;
import com.danny.service.UserService;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {

	@Resource
	private UserService userService;

	@RequestMapping("/list")
	@Cacheable(value = "user", key = "'userList'")
	public List<User> UserList() {
		List<User> users = userService.list();
		return users;
	}

	@RequestMapping("/add")
	public Boolean insertUser(@RequestBody User user) {
		boolean save = userService.save(user);
		return save;
	}

	@RequestMapping("/selectOne/{personId}")
	public User selectOne(@PathVariable Integer personId) {
		if (personId != null && personId == 0) {
			return null;
		}
		User user = userService.getById(personId);
		return user;
	}

}
